/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control.dao;

import model.Cliente;
import BD.CriaConexao;
import IRepositorios.IRepositorioCliente;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Andre
 */
public class metodosClientes implements IRepositorioCliente{
private Connection conexao;

    public metodosClientes() {
        try {
            this.conexao= CriaConexao.getConexao();
        } catch (SQLException ex) {
            Logger.getLogger(metodosClientes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void cadastrar(Cliente cliente)  {
        String sql = "insert into cliente (cpf_cli, nome, endereco) values (?, ?, ?)";
        
        try {
            try (PreparedStatement stmt = conexao.prepareStatement(sql)) {
                stmt.setLong(1, cliente.getCpf());
                stmt.setString(2, cliente.getNome());
                stmt.setString(3, cliente.getEndereco());
                stmt.execute();
            }
        } catch (SQLException ex) {
            Logger.getLogger(metodosClientes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Cliente buscar(long cpf) {
        
        int decisao=0;
        Cliente cliente= new Cliente();
        String sql="select * from cliente where cpf_cli=?";
        try {
            PreparedStatement stmt= conexao.prepareStatement(sql);
            stmt.setLong(1, cpf);
            ResultSet rs = stmt.executeQuery();
            while(rs.next()){
            
            cliente.setCpf(rs.getLong("cpf_cli"));
            cliente.setNome(rs.getString("nome"));
            cliente.setEndereco(rs.getString("endereco"));
            decisao=1;
            }
            rs.close();
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(metodosClientes.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        if(decisao==1)
        return cliente;
        else
        return null;    
    }

    @Override
    public void atualizar(Cliente a) {
        String sql = " update cliente set nome=?, endereco=? where cpf_cli=?";
        PreparedStatement stmt;
        try {
            stmt = conexao.prepareStatement(sql);
        
        
        stmt.setString(1, a.getNome());
        stmt.setString(2, a.getEndereco());
        stmt.setLong(3, a.getCpf());
        
        
        stmt.execute();
        stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(metodosClientes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void deletar(long cpf) {
        
        
        String sql_e = "delete from cliente where cpf_cli= ?";
        PreparedStatement consulta_e;
        try {
        consulta_e = conexao.prepareStatement(sql_e);
        consulta_e.setLong(1, cpf);
        consulta_e.execute();
        consulta_e.close();
        } catch (SQLException ex) {
            Logger.getLogger(metodosClientes.class.getName()).log(Level.SEVERE, null, ex);
        }
        JOptionPane.showMessageDialog(null, "Excluído com sucesso.");
        
            //JOptionPane.showMessageDialog(null, "Não é possivel excluir este cliente.");
       
        
        
    }

    @Override
    public List<Cliente> listar() {
        String sql="select cpf_cli, nome, endereco from cliente";
        PreparedStatement stmt;
        List<Cliente> minhaLista= new ArrayList<>();
        try {
            stmt = conexao.prepareStatement(sql);
        
        ResultSet rs= stmt.executeQuery();
        while(rs.next()){
            Cliente cli= new Cliente();
            cli.setNome(rs.getString("nome"));
            cli.setEndereco(rs.getString("endereco"));
            cli.setCpf(rs.getLong("cpf_cli"));
            minhaLista.add(cli);
        }
        } catch (SQLException ex) {
            Logger.getLogger(metodosClientes.class.getName()).log(Level.SEVERE, null, ex);
        }
        return minhaLista;
                
    }

    @Override
    public List<Cliente> listar(String nome) {
        String sql = "select * from cliente where nome like ? order by nome";
        PreparedStatement stmt;
        List<Cliente> minhaLista = null;
        try {
            stmt = conexao.prepareStatement(sql);
        
        stmt.setString(1, nome);
        ResultSet rs = stmt.executeQuery();
        minhaLista = new ArrayList<>();
        while (rs.next()) {
            Cliente a1 = new Cliente();
            a1.setCpf(rs.getLong("cpf_cli"));
            a1.setEndereco(rs.getString("endereco"));
            a1.setNome(rs.getString("nome"));
            
            minhaLista.add(a1);
        }
        rs.close();
        stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(metodosClientes.class.getName()).log(Level.SEVERE, null, ex);
        }
        return minhaLista;
    }

//    private boolean VerificaSeHáLocacoesReservas(long cpf) {
//        String sql="select id_op from operacao where cpf_op= ?";
//        boolean existe=false;
//        try {
//            PreparedStatement stmt= conexao.prepareStatement(sql);
//            stmt.setLong(1, cpf);
//            ResultSet rs= stmt.executeQuery();
//            while(rs.next()){
//                existe=true;
//            }
//            rs.close();
//            stmt.close();
//        } catch (SQLException ex) {
//           Logger.getLogger(metodosClientes.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return existe;
//    }
    }

    
    
    
   
